home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7900 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: news.th-darmstadt.de!news
  2. From: Enno Sandner <enno@intellektik.informatik.th-darmstadt.de>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: type-casting in multi-inheritance
  5. Date: Mon, 12 Feb 1996 09:31:31 +0100
  6. Organization: Fachbereich Informatik, TH Darmstadt
  7. Message-ID: <311EFAE3.41C67EA6@intellektik.informatik.th-darmstadt.de>
  8. References: <DMn7Ix.G9@Virginia.EDU>
  9. NNTP-Posting-Host: kitz.intellektik.informatik.th-darmstadt.de
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (X11; I; SunOS 4.1.3 sun4m)
  14.  
  15. Gregory Carl Lewin wrote:
  16. > I am a bit fuzzy about casting from pointers to derived objects
  17. > to base classes when you are using multiple inheritance.
  18. > Perhaps someone could explain the following to me.  Consider
  19. > the arrangement:
  20. > class Base1 {};
  21. > class Base2 {};
  22. > class Derived12 : public Base1, Base2 {};
  23. > Base1 b1;
  24. > Base2 b2;
  25. > Derived12 d12;
  26. > void foo(void) {
  27. > Base1* pb1;
  28. > pb1=&b1;
  29. > pb1=&d12;
  30. > Base2* pb2;
  31. > pb2=&b2;
  32. > pb2=&d12; //ERROR: Cannot convert pointer
  33. > pb2=(Base2*) &d12;  //OK: But is it ALWAYS safe?
  34. > }
  35. > Because Derived 12 is publicly derived from Base1 and Base2, I
  36. > would have expected the conversion of &d12 to a Base2* to be
  37. > automatic; however, the comiler balks.  The subsequent explicit
  38. > conversion works, and the program seems to act appropriately,
  39. > but using the explicit type-cast scares me.  Can someone
  40. > explain why this is and if this is the "correct" way around the
  41. > problem (alternatives being virtual inheritance with class Base
  42. > being a base for Base1 and Base2, and ???).
  43.  
  44. 'Derived12' inherits _private_ not public from 'Base2'.
  45. The correct definition of 'Derived12' should be
  46.  
  47.     class Derived12 : public Base1, public Base2 {};
  48.                     ^^^^^^
  49.  
  50. Now the problem should disappear.
  51.  
  52.     Enno
  53.